<template>
  <div class="cateParams">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert title="警告提示的文案" type="warning" :closable="false" show-icon></el-alert>
      <el-row class="cate_goods">
        <el-col>
          <span>商品分类：</span>
          <el-cascader
            v-model="cateKeyEd"
            :options="cateList"
            :props="props"
            @change="handleChange"
            expandTrigger="hover"
            clearable
            style="width:30%"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="静态参数" name="only">
          <el-button type="primary" size="mini" :disabled="isDisable" @click="addOnlyParams">添加参数</el-button>
          <el-table :data="onlyList" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  closable
                  @close="closeTag(scope.row,index)"
                  v-for="(item,index) in scope.row.attr_vals"
                  :key="index"
                >{{item}}</el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                ></el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                >+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="info"
                  @click="handleEditParams(scope.$index, scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDeleteParams(scope.$index, scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini" :disabled="isDisable" @click="addOnlyParams">添加参数</el-button>
          <el-table :data="manyList" style="width: 100%">
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-tag
                  closable
                  v-for="(item,index) in scope.row.attr_vals"
                  :key="index"
                  @close="closeTag(scope.row,index)"
                >{{item}}</el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                ></el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                >+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="参数名称" prop="attr_name"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="info"
                  @click="handleEditParams(scope.$index, scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDeleteParams(scope.$index, scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加参数 -->
    <el-dialog
      :title="'添加'+paramsText"
      :visible.sync="addParasmDialog"
      width="50%"
      @close="clearDialog"
    >
      <el-form ref="onlyRef" :model="addDialogOnlyParasm" :rules="onlyRules">
        <el-form-item :label="paramsText" prop="attr_name">
          <el-input class="inp" v-model="addDialogOnlyParasm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParasmDialog = false">取 消</el-button>
        <el-button type="primary" @click="addParamsOnly">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改参数 -->
    <el-dialog title="修改参数" :visible.sync="editParasmDialog" width="50%" @close="clearDialog">
      <el-form ref="onlyRef" :model="addDialogOnlyParasm">
        <el-form-item label="参数名称" prop="attr_name">
          <el-input class="inp" v-model="editDialogOnlyParasm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editParasmDialog = false">取 消</el-button>
        <el-button type="primary" @click="editParamsOnly">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { type } from 'os'
export default {
  name: 'cateParams',
  data() {
    return {
      cateList: [],
      editParasmDialog: false,
      cateKeyEd: [],
      activeName: 'only',
      isDisable: true,
      addParasmDialog: false,
      addDialogOnlyParasm: {
        attr_name: ''
      },
      editDialogOnlyParasm: {
        attr_name: '',
        attrId: ''
      },
      onlyList: [],
      manyList: [],
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      onlyRules: {
        attr_name: [
          { required: true, message: '参数不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    getCateList() {
      this.$axios.get('categories').then(result => {
        // console.log(result, 1)
        this.cateList = result.data.data
      })
    },
    handleChange() {
      // console.log(this.cateKeyEd)
      if (this.cateKeyEd.length !== 3) {
        this.cateKeyEd = []
        this.onlyList = []
        this.manyList = []
        this.isDisable = true
        return
      }
      this.isDisable = false
      // console.log(this.cateKeyEd)
      this.$axios
        .get(`categories/${this.cateKeyEd[2]}/attributes`, {
          params: { sel: [this.activeName] }
        })
        .then(result => {
          result.data.data.forEach(item => {
            // console.log(item.attr_vals, 32)
            item.inputVisible = false
            item.inputValue = ''
            if (item.attr_vals == '') {
              item.attr_vals = []
            } else {
              item.attr_vals = item.attr_vals.split(' ')
            }
          })
          if (this.activeName == 'only') {
            this.onlyList = result.data.data
            // console.log(this.onlyList)
          } else {
            this.manyList = result.data.data
            // console.log(this.manyList)
          }
        })
    },
    handleClick() {
      // console.log(this.activeName)
      this.handleChange()
    },
    addOnlyParams() {
      this.addParasmDialog = true
    },
    addParamsOnly() {
      if (this.activeName === 'only') {
        this.$refs.onlyRef.validate(valid => {
          if (valid) {
            this.$axios
              .post(`categories/${this.cateKeyEd[2]}/attributes`, {
                attr_name: this.addDialogOnlyParasm.attr_name,
                attr_sel: this.activeName
              })
              .then(result => {
                this.$message({
                  type: 'success',
                  message: result.data.meta.msg
                })
                this.handleChange()
                this.addParasmDialog = false
              })
          } else {
            this.$message({
              type: 'error',
              message: '失败'
            })
            return false
          }
        })
      } else {
        this.$axios
          .post(`categories/${this.cateKeyEd[2]}/attributes`, {
            attr_name: this.addDialogOnlyParasm.attr_name,
            attr_sel: this.activeName
          })
          .then(result => {
            this.$message({
              type: 'success',
              message: result.data.meta.msg
            })
            this.handleChange()
            this.addParasmDialog = false
          })
      }
    },
    clearDialog() {
      this.$refs.onlyRef.resetFields()
    },
    handleEditParams(index, row) {
      // console.log(row)
      this.editDialogOnlyParasm.attr_name = row.attr_name
      this.editDialogOnlyParasm.attrId = row.attr_id
      this.editParasmDialog = true
    },
    handleDeleteParams(index, row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`categories/${this.cateKeyEd[2]}/attributes/${row.attr_id}`)
            .then(result => {
              this.$message({
                type: 'success',
                message: result.data.meta.msg
              })
              this.handleChange()
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editParamsOnly() {
      this.$axios
        .put(
          `categories/${this.cateKeyEd[2]}/attributes/${
            this.editDialogOnlyParasm.attrId
          }`,
          {
            attr_name: this.editDialogOnlyParasm.attr_name,
            attr_sel: this.activeName
          }
        )
        .then(result => {
          this.$message({
            type: 'success',
            message: result.data.meta.msg
          })
          this.handleChange()
          this.editParasmDialog = false
        })
    },
    handleInputConfirm(row) {
      // console.log(row)
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        this.$message({
          type: 'warning',
          message: '字段不能为空'
        })
        return
      }
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      this.$axios
        .put(
          `categories/${this.cateKeyEd[this.cateKeyEd.length - 1]}/attributes/${
            row.attr_id
          }`,
          {
            attr_name: row.attr_name,
            attr_sel: this.activeName,
            attr_vals: row.attr_vals.join(' ')
          }
        )
        .then(result => {
          if (result.data.meta.status != 200) {
            this.$message({
              type: 'error',
              message: result.data.meta.msg
            })
            return
          }
          this.$message({
            type: 'success',
            message: result.data.meta.msg
          })
        })
    },
    showInput(row) {
      // console.log(row)
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
      // console.log('showInput')
    },
    closeTag(row, index) {
      // console.log(row, index)
      row.attr_vals.splice(index, 1)
      // console.log(row.attr_vals)
      this.$axios
        .put(
          `categories/${this.cateKeyEd[this.cateKeyEd.length - 1]}/attributes/${
            row.attr_id
          }`,
          {
            attr_name: row.attr_name,
            attr_sel: this.activeName,
            attr_vals: row.attr_vals.join(' ')
          }
        )
        .then(result => {
          if (result.data.meta.status != 200) {
            this.$message({
              type: 'error',
              message: result.data.meta.msg
            })
            return
          }
          this.$message({
            type: 'success',
            message: result.data.meta.msg
          })
        })
    }
  },
  computed: {
    paramsText() {
      if (this.activeName === 'only') {
        return '静态参数'
      } else {
        return '动态参数'
      }
    }
  }
}
</script>
<style scoped>
.cate_goods {
  margin-top: 15px;
}
.cate_goods span {
  font-size: 14px;
}
.el-tag {
  margin: 0 5px;
}
.inp {
  width: 80%;
}
.input-new-tag {
  width: 150px;
}
</style>